package cn.com.fssg.platform.base.handler.impl;

import cn.com.fssg.common.vo.DoubleVO;
import cn.com.fssg.platform.base.handler.InterfaceHandler;
import cn.com.fssg.platform.modules.engine.util.MinioUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Iterator;

/**
 * @author : xzt
 * @description :
 * @createDate : 2025/9/1 14:32
 */
public class MinioUploadDemoExtend implements InterfaceHandler {

    private MinioUtil minioUtil = MinioUtil.cur();

    @Override
    public Object run(HttpServletRequest request, JSONObject jsonObject) throws Exception {

        MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
        Iterator<String> iter = multiRequest.getFileNames();
        while (iter.hasNext()) {
            MultipartFile multipartFile = multiRequest.getFile(iter.next());
            //objectName文件路径，上传File对象的时候可不传,会自动生成自动生成。比如:"temp/a.png",所以这里建议转成FILE
            // 创建临时文件
            File tempFile = File.createTempFile("upload-", multipartFile.getOriginalFilename());
            // 将 MultipartFile 内容写入临时文件
            multipartFile.transferTo(tempFile);
            //objectType可以理解为文件夹
            DoubleVO<String, String> doubleVO = minioUtil.upload(tempFile, null,null);
            //上传minio后的文件路径
            String filePath = doubleVO.getOne();
            //版本
            String version = doubleVO.getTwo();
            System.out.println(filePath);
            System.out.println(version);
        }
        return null;
    }

    @Override
    public void run(HttpServletRequest request, HttpServletResponse response, JSONObject jsonObject) throws Exception {
    }
}

